/**
 * 
 */
package br.com.ctm.advogadope.negocio.impl;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.annotation.security.PermitAll;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.xml.namespace.QName;
import javax.xml.ws.BindingProvider;

import br.com.ctm.advogadope.dao.api.ProcessoDao;
import br.com.ctm.advogadope.model.Processo;
import br.com.ctm.advogadope.negocio.api.ProcessoNegocio;
import br.com.framework.negocio.impl.BaseNegocioImpl;
import br.com.framework.util.date.DateUtil;
import br.jus.cnj.intercomunicacao.beans.DataHora;
import br.jus.cnj.intercomunicacao.beans.NumeroUnico;
import br.jus.cnj.intercomunicacao.beans.ProcessoJudicial;
import br.jus.cnj.intercomunicacao.beans.RequisicaoConsultaProcesso;
import br.jus.cnj.intercomunicacao.beans.RespostaConsultaProcesso;
import br.jus.cnj.intercomunicacao.servico.Intercomunicacao;
import br.jus.cnj.intercomunicacao.servico.IntercomunicacaoService;

/**
 * @author Cleber
 *
 */
@Stateless
@PermitAll
public class ProcessoNegocioImpl extends BaseNegocioImpl<Processo, ProcessoDao> implements
		ProcessoNegocio {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private IntercomunicacaoService service;
	private Intercomunicacao pje;

	/**
	 * 
	 */
	public ProcessoNegocioImpl() {
	}
	
	@PostConstruct
	protected void initService() {
		try {
			service = new IntercomunicacaoService(
				new URL("http://pje.trt13.jus.br/primeirograu/intercomunicacao?WSDL"), 
				new QName("http://www.cnj.jus.br/servico-intercomunicacao-2.1/", "IntercomunicacaoService"));
			pje = service.getPort(Intercomunicacao.class);
			Map<String, Object> context = ((BindingProvider)pje).getRequestContext();
			context.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "http://pje.trt13.jus.br/primeirograu/intercomunicacao");
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Inject
	protected void setDao(ProcessoDao dao) {
		super.setDao(dao);
	}

	/* (non-Javadoc)
	 * @see br.com.ctm.advogadope.negocio.api.ProcessoNegocio#buscarProcessoJudicial(java.lang.String, boolean, java.util.Date)
	 */
	public ProcessoJudicial buscarProcessoJudicial(String npu, boolean movimentos, Date dataReferencia) {
		RequisicaoConsultaProcesso req = new RequisicaoConsultaProcesso();
		req.setIdConsultante("123");
		req.setSenhaConsultante("123");
		NumeroUnico numUnico = new NumeroUnico();
		numUnico.setValue(npu);
		req.setNumeroProcesso(numUnico);
		req.setMovimentos(movimentos);
		if (dataReferencia != null) {
			DataHora dataHora = new DataHora();
			dataHora.setValue(DateUtil.formatDate(dataReferencia, "yyyyMMddHHmmss"));
			req.setDataReferencia(dataHora);
		}
		RespostaConsultaProcesso res = pje.consultarProcesso(req);
		return res.getProcesso();
	}
}
